﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GraphSharp.Controls;
using Microsoft.Win32;
using RWGUI.Model;
using RWGUI.ViewModel;
using RWLibrary.Logic.Queries;
using RWLibrary.Parser;

namespace RWGUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MainWindowViewModel vm;
        public MainWindow()
        {
            
            //PocGraphLayout l = new PocGraphLayout();
            //l.Graph = vm.Graph;
            //PocGraphLayout.eg
            //l.EdgeRoutingAlgorithmType = l.EdgeRoutingAlgorithmFactory.AlgorithmTypes.ElementAt(0);
            vm = new MainWindowViewModel();
            this.DataContext = vm;
            InitializeComponent();
            
        }




        private void Button_Click(object sender, RoutedEventArgs e)
        {
            vm.ReLayoutGraph();
            queryComboBox.Items.Clear();
        }

        private void LoadMenuItem_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "World descriptions (.world)|*.world";
            if(openFile.ShowDialog() == true)
            {
                using (StreamReader sr= new StreamReader(openFile.FileName) )
                {
                    vm.Statement = sr.ReadToEnd();
                }
                
            }
        }

        private void SaveMenuItem_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Filter = "World descriptions (.world)|*.world";
            if (saveFile.ShowDialog() == true)
            {
                using (StreamWriter sw = new StreamWriter(saveFile.FileName))
                {
                    sw.Write(stmTB.Text);
                }

            }
        }

        private void ExitMenuItem_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void RunButton_Click(object sender, RoutedEventArgs e)
        {
            string s = queryComboBox.Text;
            QueryParser queryParser = new QueryParser(vm.ModelGraph);
            Query q = null;
            try
            {
                q = queryParser.ParseQuery(s);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error in query parsing.");
                return;
            }

            string resultMsg = string.Format("Query returned {0}", q.Excecute());

            MessageBox.Show(resultMsg, "Result of query.");
            queryComboBox.Items.Add(new ComboBoxItem{Content = s} );
        }


        /*
        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            vm.Statements.Add(newS.Text);
        }

        private void DelButton_Click(object sender, RoutedEventArgs e)
        {
            string st = statements.SelectedItem as string;
            if (st != null)
                vm.Statements.Remove(st);
        }
          */
    }
}
